package sddz.foodmanage.fragment;


import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import sddz.foodmanage.R;
import sddz.foodmanage.base.BaseFragment;
import sddz.foodmanage.utils.SpfUtils;

/**
 * @Date:2019/7/3
 * @Author:lbb 订单
 */
public class OrderformFragment extends BaseFragment {


    @BindView(R.id.order_tab)
    TabLayout orderTab;
    Unbinder unbinder;
    @BindView(R.id.title_navigation_icon)
    ImageView titleNavigationIcon;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.title_menu_icon)
    ImageView titleMenuIcon;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.image_title_right)
    ImageView imageTitleRight;
    @BindView(R.id.title_layout)
    AppBarLayout titleLayout;
    @BindView(R.id.order_viewPager)
    ViewPager orderViewPager;
    private List<Fragment> mList = new ArrayList<>();
    private FragmentStatePagerAdapter adapter;

    public OrderformFragment() {
        // Required empty public constructor
    }

//    @Override
//    public void onDetach() {
//        super.onDetach();
//        try {
//            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
//            childFragmentManager.setAccessible(true);
//            childFragmentManager.set(this, null);
//
//        } catch (NoSuchFieldException e) {
//            throw new RuntimeException(e);
//        } catch (IllegalAccessException e) {
//            throw new RuntimeException(e);
//        }
//    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_orderform, container, false);
        unbinder = ButterKnife.bind(this, view);
        initData();
        initView();

        initListener();
        view.setClickable(true);
        return view;
    }

    private void initData() {
        adapter = new FragmentStatePagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mList.get(position);
            }

            @Override
            public int getCount() {
                return mList.size();
            }
        };

        orderViewPager.setAdapter(adapter);

    }

    private void initView() {
        titleNavigationIcon.setVisibility(View.GONE);
        titleText.setText("订单");

        if (SpfUtils.getUserData(mActivity).getType() == 2) {
            orderTab.setVisibility(View.VISIBLE);
            TabLayout.Tab tab1 = orderTab.newTab();
            TabLayout.Tab tab2 = orderTab.newTab();
            TabLayout.Tab tab3 = orderTab.newTab();
            TabLayout.Tab tab4 = orderTab.newTab();
            tab1.setText("待核实");
            tab2.setText("待评论");
            tab3.setText("已完成");
            tab4.setText("其他");
            orderTab.addTab(tab1);
            orderTab.addTab(tab2);
            orderTab.addTab(tab3);
            orderTab.addTab(tab4);
            /**
             * 中队的待核实(1)，待评论页面(2)（订单号，时间 金额）
             * 供应商的待配送（(3)中队 时间 金额）
             * 管理员的订单页面(1)
             * 中队的其他页面(2)
             * 供应商的已完成页面(3)
             * 中队的已完成页面（没有中队的选择）(4)
             */
            WaitFragment waitFragment = new WaitFragment();
            // 通过bundle传参数
            Bundle bundle = new Bundle();
// 通过put方法加入参数 根据数据的类型使用对应的方法
            bundle.putString("types", "待核实");
            waitFragment.setArguments(bundle);
            mList.add(WaitFragment.newInstance("待核实"));
            mList.add(WaitFragment.newInstance("待评论"));
            mList.add(DoneOrderFragment.newInstance("已完成"));
            mList.add(DoneOrderFragment.newInstance("其他"));
            orderTab.getTabAt(0).select();
            adapter.notifyDataSetChanged();
        } else if (SpfUtils.getUserData(mActivity).getType() == 1) {//管理员
            orderTab.setVisibility(View.GONE);
            mList.add(DoneOrderFragment.newInstance("订单"));
            adapter.notifyDataSetChanged();

        } else if (SpfUtils.getUserData(mActivity).getType() == 3) {//供
            // 应商
            orderTab.setVisibility(View.VISIBLE);
            TabLayout.Tab tab1 = orderTab.newTab();
            TabLayout.Tab tab2 = orderTab.newTab();

            tab1.setText("待配送");
            tab2.setText("已完成");
            orderTab.addTab(tab1);
            orderTab.addTab(tab2);
            /**
             * 中队的待核实(1)，待评论页面(2)（订单号，时间 金额）
             * 供应商的待配送（(3)中队 时间 金额）
             */
            mList.add(WaitFragment.newInstance("待配送"));
            mList.add(DoneOrderFragment.newInstance("供应商完成"));
            orderTab.getTabAt(0).select();
            adapter.notifyDataSetChanged();
        }
    }

    protected void initListener() {
        orderViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(orderTab));

        orderTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.e("点击第",tab.getPosition()+"");
                orderViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }

    @OnClick({R.id.title_navigation_icon, R.id.order_tab})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_navigation_icon:
                break;
            case R.id.order_tab:
                break;
        }
    }


}
